package com.skt.casdemo.view;

import java.io.IOException;
import java.lang.reflect.Method;

import com.skt.casdemo.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class FakeCallActivity extends Activity{
	
	private SoundPool soundPool;
	private int soundID;
	boolean loaded = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
        setContentView(R.layout.fakecall_activity);
        
        final Button button = (Button)findViewById(R.id.FakeCallSaveButton);
        final EditText name = (EditText)findViewById(R.id.FakeCallName);
        final EditText phone = (EditText)findViewById(R.id.FakeCallPhone);
        final SharedPreferences settings = getPreferences(MODE_WORLD_READABLE);
        
        name.setText(settings.getString("FakeCallName", "Enter Name Here: "));
        phone.setText(settings.getString("FakeCallPhone", "000-000-0000"));
        
        this.setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
        
        soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        //soundID = soundPool.load(this, Settings.System.DEFAULT_RINGTONE_URI, 1);
        
        
        button.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// Perform action on click
				SharedPreferences.Editor editor = settings.edit();
				editor.putString("FakeCallName", name.getText().toString());
				editor.putString("FakeCallPhone", phone.getText().toString());
				editor.commit();
				//Toast.makeText(getApplicationContext(), "Saved Data", Toast.LENGTH_SHORT).show();
				//Toast.makeText(getApplicationContext(), name.getText().toString(), Toast.LENGTH_SHORT).show();
				PlayRingtone();
				
			}
		});
	}
	
	private void PlayRingtone(){
		MediaPlayer player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
		
		if(player == null){
			Toast.makeText(getApplicationContext(), "Cannot Play Ringtone", Toast.LENGTH_SHORT).show();
			return;
		}
		try{
			player.prepare();
		}catch(IllegalStateException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		player.start();
	}
	
	private void SoundPoolExample(){
		//soundPool.play(soundID, volume, volume, , loop, rate);
	}
}
